import Koa from "koa";
import bodyParser from "koa-bodyparser";
import routes from "./routes/index.js";
import { handleErr } from "./controller/handleErr.js";
// 需要redis则取消注释
// import { redis } from "./db/index.js";
import { pid } from "node:process";
import { writeFileSync } from "node:fs";

async function main() {
  writeFileSync("./pid.log", pid.toString(), { encoding: "utf-8" });
  // 需要redis则取消注释
  // await redis.connect();
  const app = new Koa();
  console.log("pid", pid);
  app.on("error", (err, ctx) => {
    console.log("server error", err);
    ctx.status = 500;
    ctx.body = "QAQ";
  });
  app.use(handleErr);
  app.use(bodyParser());
  app.use(routes.routes());
  app.use(routes.allowedMethods());
  app.listen(8009);
}

main();
